define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'device/index' + location.search,
                    add_url: 'device/add',
                    edit_url: 'device/edit',
                    deviceset_url: 'device/deviceset',
                    cpp_url: 'device/cpp',
                    yl_url: 'device/yl',
                    dg_url: 'device/dg',
                    del_url: 'device/del',
                    multi_url: 'device/multi',
                    import_url: 'device/import',
                    table: 'device',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'ground.name', title: __('Ground.name'), operate: 'LIKE', table: table, class: 'autocontent',
                            formatter: function (value, row, index) {
                                if(!row.ground.name){
                                    return '未关联';
                                }else{
                                    return row.ground.name;
                                }
                            }
                        },
                        {field: 'user.nickname', title: __('所属用户'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'type', title: __('Type'), searchList: {"online":__('Type online'),"offline":__('Type offline'),"water_warning":__('Type water_warning'),"ele_warning":__('Type ele_warning'),"all_faults":__('Type all_faults')}, formatter: Table.api.formatter.normal},
                        {field: 'yj_status', title: __('设备预警'), searchList: {"0":__('无'),"1":__('水位过低预警'),"2":__('电压过低预警'),"3":__('所有故障')}, formatter: Table.api.formatter.normal},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'ground_id', title: __('Ground_id')},
                        {field: '', title: __('设备信息'),
                            formatter: function (value, row, index) {
                                var yinliang = '音量:' + row.yinliang + '<br>';
                                if(row.yl_timed){
                                    var yinliu = '引流信息:' + row.yl_timed + '~'+row.yl_time_end+'<br>';
                                }else {
                                    var yinliu = '引流信息:未设置<br>';
                                }
                                if(row.yl_djjg){
                                    var cpp = '出泡泡间隔:' + row.yl_djjg + ' 时长：'+row.yl_cpp+'<br>';
                                }else{
                                    var cpp = '出泡泡间隔:未设置<br>';
                                }
                                var dg = '彩灯时间段:' + row.dg_start  + '~'+row.dg_end+'<br>';
                                if(row.dg_start){
                                    var dg = '彩灯时间段:'+row.dg_start+'~'+row.dg_end+'<br>';
                                }else{
                                    var dg = '彩灯时间段:未设置<br>';

                                }
                                return yinliang  + yinliu + cpp + dg;
                            },
                            operate:false
                        },
                        {field: 'ele', title: __('Ele'), operate: 'BETWEEN', sortable: true},
                        {field: 'ewm', title: __('二维码'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'active_code', title: __('活动码'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},

                        {field: 'led_kg', title: __('灯开关'),operate:false, searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'music_kg', title: __('音乐开关'),operate:false, searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'dj_kg', title: __('电机开关'), operate:false,searchList: {"1": __('Yes'), "0": __('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'panttime', title: __('Panttime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'deviceset',
                                    text:'引流设置',//按钮名称
                                    title:'引流设置',//按钮名称
                                    classname: 'btn btn-xs btn-success btn-view btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'device/deviceset?id='+row.id
                                    },
                                },
                                {
                                    name: 'cpp',
                                    text:'出泡泡',//按钮名称
                                    title:'出泡泡',//按钮名称
                                    classname: 'btn btn-xs btn-success btn-view btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'device/cpp?id='+row.id
                                    },
                                },
                                {
                                    name: 'yl',
                                    text:'音量控制',//按钮名称
                                    title:'音量控制',//按钮名称
                                    classname: 'btn btn-xs btn-success btn-view btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'device/yl?id='+row.id
                                    },
                                },
                                {
                                    name: 'dg',
                                    text:'灯光控制',//按钮名称
                                    title:'灯光控制',//按钮名称
                                    classname: 'btn btn-xs btn-success btn-view btn-dialog',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'device/dg?id='+row.id
                                    },
                                },

                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'device/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'device/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'device/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deviceset: function () {
            Controller.api.bindevent();
        },
        cpp: function () {
            Controller.api.bindevent();
        },
        yl: function () {
            Controller.api.bindevent();
        },
        dg: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
